package md.lanjing.com.module_wallet.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.lxh.baselibray.constant.ARouterConfig;
import com.lxh.baselibray.constant.Constant;
import com.lxh.baselibray.dialog.AlertDialog;
import com.lxh.baselibray.event.EventImpl;
import com.lxh.baselibray.mvp.MvpFragment;
import com.lxh.baselibray.util.ARouterUtils;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

import md.lanjing.com.module_wallet.R;
import md.lanjing.com.module_wallet.R2;
import md.lanjing.com.module_wallet.bean.DealCenterNacVacancyResponce;
import md.lanjing.com.module_wallet.bean.QueryIncomeResponce;
import md.lanjing.com.module_wallet.contact.MyWalletContact;
import md.lanjing.com.module_wallet.ui.activity.ChargeCoinActivity;
import md.lanjing.com.module_wallet.ui.activity.DrawCoinActivity;
import md.lanjing.com.module_wallet.ui.activity.ExChangeActivity;
import md.lanjing.com.module_wallet.ui.activity.RecordActivity;
import md.lanjing.com.module_wallet.ui.activity.TransferAccountActivity;
import retrofit2.Response;



public class MyWalletFragment extends MvpFragment<MyWalletContact.MyWalletPresent> implements MyWalletContact.IMyWalletView {


    @BindView(R2.id.tv_usdt_value)
    TextView tvUsdtValue;
    @BindView(R2.id.tv_cawt_value)
    TextView tvCawtValue;
    @BindView(R2.id.tv_recomend_profit)
    TextView tvRecomendProfit;
    @BindView(R2.id.tv_group_profit)
    TextView tvGroupProfit;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;


    @Override
    public void initData(Bundle savedInstanceState) {
        mPresent.getVacancy(context);
        mPresent.queryIncome(context);

        refresh.setEnableLoadMore(false);
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresent.getVacancy(context);
                mPresent.queryIncome(context);
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_my_wallet;
    }



    @OnClick({R2.id.tv_record, R2.id.charge_coin, R2.id.tv_draw_coin, R2.id.tv_transfer_coin, R2.id.tv_exchange})
    public void onViewClicked(View view) {
        String realName = SPUtils.getString(Constant.IS_REAL, null, context);
        String purAddress = SPUtils.getString(Constant.PURSE_ADDRESS, null, context);
        int i = view.getId();
        if (i == R.id.tv_record) {
            startActivity(new Intent(context, RecordActivity.class));

        } else if (i == R.id.charge_coin) {
  if (ObjectUtils.isEmpty(purAddress)){
                   showRemindDialog("您还未绑定钱包地址,去绑定?",1);
                    return;
                }

            startActivity(new Intent(context, ChargeCoinActivity.class));
        } else if (i == R.id.tv_draw_coin) {
            if (ObjectUtils.isEmpty(realName)) {//已经实名认证的情况下，就不需要跳转了
                showRemindDialog("你还未实名认证,去认证?", 2);
                return;
            }
            if (ObjectUtils.isEmpty(purAddress)) {
                showRemindDialog("您还未绑定钱包地址,去绑定?", 1);
                return;
            }
            startActivity(new Intent(context, DrawCoinActivity.class));
        } else if (i == R.id.tv_transfer_coin) {
            if (ObjectUtils.isEmpty(realName)) {//已经实名认证的情况下，就不需要跳转了
                showRemindDialog("你还未实名认证,去认证?", 2);
                return;
            }
            startActivity(new Intent(context, TransferAccountActivity.class));
        } else if (i == R.id.tv_exchange) {
            startActivity(new Intent(context, ExChangeActivity.class));
        }
    }

    @Override
    public void getVacancyData(Response<DealCenterNacVacancyResponce> response) {
        refresh.finishRefresh();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            DealCenterNacVacancyResponce.DataBean data = response.body().getData();
            if (ObjectUtils.isEmpty(data)) return;
            tvUsdtValue.setText(data.getUsdtNum()+"");
            tvCawtValue.setText(data.getCawdNum()+"");
            SPUtils.putString(Constant.PURSE_ADDRESS,data.getReceiveAddress(),context);
            SPUtils.putString(Constant.WEIXIN_PNG,data.getWeChatQrcode(),context);
            SPUtils.putString(Constant.ALI_PNG,data.getAlipayQrcode(),context);
            SPUtils.putString(Constant.BANK_NAME,data.getBankName(),context);
            SPUtils.putString(Constant.BANK_NUMBER,data.getBankCard(),context);
            SPUtils.putString(Constant.BANK_OWNER,data.getOwnerName(),context);
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    public void getQueryIncomeData(Response<QueryIncomeResponce> response) {
        refresh.finishRefresh();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            QueryIncomeResponce.DataBean data = response.body().getData();
            if (ObjectUtils.isEmpty(data)) return;
            tvGroupProfit.setText(data.getCommProfit()+"(CAWD)");
            tvRecomendProfit.setText(data.getIndirectProfit()+"(CAWD)");
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected MyWalletContact.MyWalletPresent createPresent() {
        return new MyWalletContact.MyWalletPresent();
    }

    AlertDialog remindDialog = null;
    private void showRemindDialog(String content,int toWhere) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .addDefaultAnimation()
                .setCancelable(true)
                .setContentView(R.layout.dialog_reimind)
                .setText(R.id.tv_content, content)
                .setWidthAndHeight(SizeUtils.dp2px(context, 279), SizeUtils.dp2px(context, 157))
                .setOnClickListener(R.id.tv_sure, v -> {
                    if (toWhere==1){
                        ARouter.getInstance().build(ARouterConfig.Mine_bind_purse_address_act).navigation();
                    }else {
                        ARouter.getInstance().build(ARouterConfig.Mine_real_name_authen_act).navigation();
                    }
                    remindDialog.dismiss();
                }).setOnClickListener(R.id.tv_cancle, v -> remindDialog.dismiss());
        remindDialog = builder.create();
        remindDialog.show();
    }

    @Override
    public boolean useEventBus() {
        return true;
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void transferReceive(EventImpl.TransferEvent event){
        if (!ObjectUtils.isEmpty(event)){
            mPresent.getVacancy(context);
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void exchangeReceive(EventImpl.ExchangeEvent event){
        if (!ObjectUtils.isEmpty(event)){
            mPresent.getVacancy(context);
        }
    }
}
